package com.hpjz.basicofing.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import lombok.extern.log4j.Log4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hpjz.basicofing.service.IUserService;
import com.hpjz.basicofing.web.vo.UserVO;
import com.hpjz.common.ParameterBean;
import com.hpjz.common.ResultBean;
import com.hpjz.common.utils.CodeUtils;
import com.hpjz.common.utils.ConstantDefinition;

@Controller
@RequestMapping("/user")
@Log4j
public class UserController {
	
	/**
	 * 用户service
	 */
	@Resource
	private IUserService userService;
	
	
	/**
	 * 添加用户
	 * @param pb
	 * @return
	 */
	@RequestMapping(value="/insertUser",method = RequestMethod.POST)
	public @ResponseBody ResultBean insertUser(@RequestBody ParameterBean pb){
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 String pwd = objMap.getPwd();
			 if (StringUtils.isNotBlank(pwd)) {
				 pwd =  CodeUtils.strToMD5(pwd);
				 objMap.setPwd(pwd);
			 }
			 boolean isInsert = this.userService.insertUser(objMap);
			 if (isInsert) {
				 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
			}else{
				result.setStatus(ConstantDefinition.SYETEM_DATA_REPEAT);
			}
			  
		} catch (Exception e) {
			 log.error("添加用户信息失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}

	/**
	 * 查询用户信息
	 * @param pb
	 * @return
	 */
	@RequestMapping(value="/findUser",method = RequestMethod.POST)
	public @ResponseBody ResultBean findUser(@RequestBody ParameterBean pb){
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 UserVO uvo =  this.userService.findUser(objMap);
			 result.setResult(uvo);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("查询用户信息失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	/**
	 * 修改用户信息
	 * @param pb
	 * @return
	 */
	@RequestMapping(value="/updateUser",method = RequestMethod.POST)
	public @ResponseBody ResultBean updateUser(@RequestBody ParameterBean pb){
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 String pwd = objMap.getPwd();
			 if (StringUtils.isNotBlank(pwd)) {
				 pwd =  CodeUtils.strToMD5(pwd);
				 objMap.setPwd(pwd);
			 }
			  this.userService.updateUser(objMap);
			  result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
			 
		} catch (Exception e) {
			 log.error("查询用户信息失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	
	
	 /**
	  * 删除用户信息
	  * @param pb
	  * @return
	  */
	@RequestMapping(value="/deleteUser",method = RequestMethod.POST)
	public @ResponseBody ResultBean deleteUser(@RequestBody ParameterBean pb){
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 this.userService.deleteUser(objMap);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("删除用户信息失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
	 /**
	  * 查询所有用户信息
	  * @param pb
	  * @return
	  */
	@RequestMapping(value="/findAllUsers",method = RequestMethod.POST)
	public @ResponseBody ResultBean findAllUsers(@RequestBody ParameterBean pb){
		UserVO objMap = (UserVO) pb.getParameter(UserVO.class);
		ResultBean result = new ResultBean();
		 try {
			 List<UserVO> uvos =  this.userService.findAllUsers();
			 result.setResult(uvos);
			 result.setStatus(ConstantDefinition.SYSTEM_SUCCESS);
		} catch (Exception e) {
			 log.error("查询用户信息失败！",e);
			 result.setStatus(ConstantDefinition.SYSTEM_ERROR);
		}
		 return result;
	}
	
}
